//
//  ALNewFeatureController.swift
//  ALSinaWeiBoSwift
//
//  Created by 柴瑞军 on 16/4/12.
//  Copyright © 2016年 myCompany. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class ALNewFeatureController: UICollectionViewController {

//  collectionviewController在创建的的时候一定要有流布局参数
    let layout = UICollectionViewFlowLayout()
    init(){
    
        super.init(collectionViewLayout: layout)
        
//        设置layout参数
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(ALHewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    // MARK: UICollectionViewDataSource

//    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//        // #warning Incomplete implementation, return the number of sections
//        return 0
//    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return NumberOfNewFeatureItem
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ALHewFeatureCell
    
        // Configure the cell
    cell.index = indexPath.item
        return cell
    }

}
// MARK:- 在一个文件中写多个类
class ALHewFeatureCell:UICollectionViewCell{
//定义属性观察器----当cell展示的item一旦发生改变的时候
    var index : Int = 0{
        didSet{
        
//            index的值有外界cell的item来决定
            let imageName = "new_feature_\(index + 1)"
//            ALPrint("imageName:\(imageName)")
            bkImaheView.image = UIImage.init(named: imageName)
            // 让按钮隐藏
            startButton.hidden = true
        }
    
    }
//    重写下面的方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepara()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    添加控件到cell上
    private func prepara(){
        // 1.添加控件
        // 当是cell的时候,view都添加到contentView

        contentView.addSubview(bkImaheView)
        contentView.addSubview(startButton)
//        添加约束
        bkImaheView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        
        // 开始按钮
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }

    }
//    按钮开始动画
    private func startAnimation(){
    
//    设置按钮不影藏
        startButton.hidden = false
//        将transform的缩放改为0  
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue:0), animations: { 
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) in
                ALPrint("动画完成了")
        }
    
    }
    
//    懒加载背景图片
    private lazy var bkImaheView:UIImageView = UIImageView.init(image:UIImage.init(named: "new_feature_1"))
    
//    懒加载按钮
    private lazy var startButton:UIButton = {
    let startButton = UIButton()
        
//        设置btn的属性
        // 设置
        startButton.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        startButton.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        
        startButton.setTitle("开始体验", forState: UIControlState.Normal)
        startButton.titleLabel?.font = welcomeFont
        
        startButton.addTarget(self, action: #selector(ALHewFeatureCell.didClickStartButton), forControlEvents: UIControlEvents.TouchUpInside)
        
        
        startButton.sizeToFit()
        
        return startButton
    }()
    
    @objc private func didClickStartButton(){
//    切换控制器界面
        let controller = ALMainTabBarController()
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(controller)
    }
}

//类的扩展一般来实现代理方法
extension ALNewFeatureController{
//当scrollview停止滚动的时候,让startButton进行动画
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x/UIScreen.mainScreen().bounds.width)
        
        if index == NumberOfNewFeatureItem-1 {
// 取出当前的cell
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! ALHewFeatureCell
            
//            让cell开始执行动画
            cell.startAnimation()
        }
    }
}